package com.wego.controller.manager;

import com.wego.controller.manager.base.BaseController;
import com.wego.service.base.BaseService;
import com.wego.common.bean.PageBean;
import com.wego.common.bean.ResultBean;
import com.wego.common.enumeration.ResultEnum;
import com.wego.common.minio.MinioUtil;
import com.wego.common.utils.QueryUtil;
import com.wego.common.utils.ResultBeanUtil;
import com.wego.constant.WeGoConst;
import com.wego.converter.AdminConverter;
import com.wego.entity.domain.Admin;
import com.wego.entity.query.AdminQuery;
import com.wego.entity.vo.manager.AdminVO;
import com.wego.service.AdminService;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 管理员控制器
 *
 * @author hc
 */
@Controller
@RequestMapping("/manager/admin")
public class AdminController extends BaseController<Admin> {
    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminConverter adminConverter;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 不提供默认的构造方法，限制其子类必须调用该方法为baseService赋值
     * @param service
     */
    public AdminController(BaseService<Admin> service) {
        super(service);
    }

    /**
     * 省份列表
     *
     * @param adminQuery
     * @param model
     * @param resultBean
     * @return
     */
    @GetMapping("/list")
    public String list(AdminQuery adminQuery, Model model, @ModelAttribute("resultBean") ResultBean resultBean) {
        //构造动态查询条件
        final String keywords = adminQuery.getKeywords();
        if (keywords != null) {
            if (adminQuery.getNickname() == null) {
                adminQuery.setNickname(keywords);
            }
            if (adminQuery.getAccount() == null) {
                adminQuery.setAccount(keywords);
            }
            if (adminQuery.getPhone() == null) {
                adminQuery.setPhone(keywords);
            }
        }

        PageBean<Admin> adminPageBean = adminService.selectPage(adminQuery);
        //转换
        final PageBean<AdminVO> pageBean = adminConverter.adminPageBean2AdminVOPageBean(adminPageBean);

        //分页时携带上查询条件
        String params = QueryUtil.genQueryCondition(adminQuery);
        pageBean.setUrl("manager/admin/list?" + params);
        model.addAttribute("pageBean", pageBean);

        //查询条件数据回显时使用
        model.addAttribute("adminQuery", adminQuery);

        //增删改操作完返回到列表页面的提示信息
        if (resultBean != null) {
            model.addAttribute("resultBean", resultBean);
        }

        return "manager/admin";
    }

    /**
     * 判断指定账户的管理员是否存在
     * @param account
     * @return
     */
    @ResponseBody
    @GetMapping("/checkAccount")
    public ResultBean checkAccount(String account) {
        boolean exist = adminService.accountExist(account);
        if (exist) {
            //账户已经存在，请重新注册
            return ResultBeanUtil.build(ResultEnum.USER_HAS_EXISTED);
        } else {
            //账户不存在，可以使用
            return ResultBeanUtil.build(ResultEnum.USER_NOT_EXIST);
        }
    }

    /**
     * 具体执行添加修改操作
     * @param admin
     * @return
     */
    @PostMapping("/doAddUpdate")
    public String doAddUpdate(Admin admin, MultipartFile avatarFile, RedirectAttributes redirectAttributes) {
        //上传图片
        if (avatarFile != null && avatarFile.getSize() > 0) {
            final String picFileName = minioUtil.putObject2(avatarFile, WeGoConst.RESOURCES_SERVER_PATH+"/admin", null);
            admin.setAvatar(picFileName);
        }

        admin.setUpdateTime(LocalDateTime.now());

        ResultBean resultBean;
        Long id = admin.getId();
        if (id != null) {
            //修改
            boolean res = adminService.updateByPrimaryKeySelective(admin);
            resultBean = ResultBeanUtil.build(res, ResultEnum.UPDATE_SUCCESS, ResultEnum.UPDATE_EXCEPTION);
        } else {
            //添加
            admin.setCreateTime(LocalDateTime.now());
            boolean res = adminService.insertSelective(admin);
            resultBean = ResultBeanUtil.build(res, ResultEnum.INSERT_SUCCESS, ResultEnum.INSERT_EXCEPTION);
        }

        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        //重定向
        return "redirect:list";
    }
}
